// pages/addBox/addVideo/addVideo.js
var common = require('../../../service/common.js');
var user = require('../../../service/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  cover:"",
  video:"",
  index:-1,
  edit:"-1",
  DataList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.edit == "1"){//修改

    }else{//增加
      this.setData({
        index: options.index,
        edit: options.edit,
        DataList: wx.getStorageSync('DataList')
      })
    }
  },
// 上传封面图
  uploadImg:function(){
    wx.chooseImage({
      count:1,
      success: (res) => {
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        if (tempFilePaths.length > 0) {
          wx.showLoading({
            title: '上传中...',
          })
          user.getToken(getApp().globalData.userInfo.openid,token=>{
            wx.uploadFile({
              url: common.getUrl("website.uploadImg"),
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                token: token
              },
              success: (res) => {
                var data = JSON.parse(res.data)
                if (data.code == 0) {
                  wx.hideLoading()
                  this.setData({
                    cover: data.data.url
                  })
                }
              }
            })
          })
          
        }

      }
    })
  },

  // 上传视频
  uploadVideo:function(){
    
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success:  (res)=>{
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: common.getUrl('website.uploadVideo'), //仅为示例，非真实的接口地址
          filePath: res.tempFilePath,
          name: 'file',
          formData: {
            'token': user.getToken(getApp().globalData.userInfo.openid)
          },
          success: (ress) => {
            var data = JSON.parse(ress.data)
            if (data.code==0) {
              wx.hideLoading()
              this.setData({
                video: data.data.url
              })
              console.log(data.data.url)
            }
          }
        })
      }
    })
  },
  save:function(){
    if(this.data.cover == ""){
        wx.showToast({
          image:"/image/wrong.png",
          title: '请上传视频封面图',
        })
    } else if (this.data.video == ""){
      wx.showToast({
        image: "/image/wrong.png",
        title: '请上传视频',
      })
    }else{
      if(this.data.edit==1){
        console.log(this.data.video)
        this.data.DataList[index].content = this.data.video
        this.data.DataList[index].cover = this.data.cover
      }else{
        console.log(this.data.video)
        var data = {
          types: 4,
          content: [this.data.video],
          cover: this.data.cover
        }
        console.log(this.data.DataList)
        this.data.DataList.splice(this.data.index, 0, data)
      }
      wx.setStorageSync("DataList", this.data.DataList)
      setTimeout(() => {
        wx.navigateBack()
      }, 500)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})